import JayUtil from "../../Fantasy/Util/JayUtil";
import Label from "../../Fantasy/Component/Label";
import ServerCenter from "../../Server/ServerCenter";
import QuestSpawn from "../QuestSpawn";
import SoundUtil from "../../Fantasy/Util/SoundUtil";

const {ccclass, property} = cc._decorator;
const leterNum = 18;
;
@ccclass
export default class WriteQuestJs extends cc.Component {

    @property(cc.Node)
    tip:cc.Node = null;
    @property(cc.Node)
    lineNode:cc.Node = null;
    @property(cc.Node)
    buttonArr:Array<cc.Node> = [];
    @property(cc.Prefab)
    linePre:cc.Node = null;
    @property(cc.Prefab)
    writeButtonPre:cc.Prefab = null;
    LetterArr = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
    wordArr;
    answerIndex;
    answerArr = [];
    lineArr
    gameJs;
    questExp;
    buttonLabel = [];//按钮上面的文字
    LabelButtonArr;//文字按钮的数组
    isUpperCase = false;
    onEnable(){
        if(QuestSpawn.isWrong == true){
            for(let i=0;i<this.lineArr.length;i++){
                this.lineArr[i].getChildByName("label").getComponent(cc.Label).string = this.wordArr[i]
            }
        }
    }
    init(nodeJs,wordsList){
        this.LabelButtonArr = [];
        Log(this.LabelButtonArr)
        this.gameJs = nodeJs;
        this.questExp = 7;
        var tempArr=wordsList.slice(0);
        var random=Math.floor(Math.random()*(tempArr.length-1));
        this.tip.getChildByName("label").getComponent(cc.Label).string = tempArr[random].translation
        this.wordArr = tempArr[random].words.split("");
        Log("题目的字母数组",this.wordArr);
        if(this.wordArr.length > 7){
            this.init(nodeJs,wordsList);
        }else{
            for(let i=0;i< this.wordArr.length;i++){
                let answerLine = cc.instantiate(this.linePre);
                answerLine.parent = this.lineNode;
                this.answerArr.push("");
            }
            this.lineArr = this.lineNode.children;
            this.setkeyboard(this.wordArr);
            this.updateAnawer();
        }

    }
    updateAnawer(){
        for(let i = 0;i<this.answerArr.length;i++){
            if(this.answerArr[i] == ""){
                this.answerIndex = i;
                Log(this.answerIndex);
                return;
            }
        }
    }

    setkeyboard(wordArr){
        var  keyArr = this.getkeyWords(this.LetterArr,(leterNum - this.wordArr.length));
        let newKeyArr = JayUtil.shuffle(keyArr.concat(this.wordArr));
        this.buttonLabel = newKeyArr;
        Log("键盘的字母",newKeyArr);
        this.initKey(newKeyArr);
        
    }

    initKey(newKeyArr){
        if(this.LabelButtonArr.length == 0 ){//== null
            for(let i = 0;i< newKeyArr.length;i++){
                let line = i / 6;
                if(line < 1){
                    let node = cc.instantiate(this.writeButtonPre);
                    this.LabelButtonArr.push(node);
                    node.getComponent("WriteButtonJs").init(newKeyArr[i]);
                    node.parent = this.buttonArr[0];
                }else if(line >= 1 && line < 2){
                    let node = cc.instantiate(this.writeButtonPre);
                    this.LabelButtonArr.push(node);
                    node.getComponent("WriteButtonJs").init(newKeyArr[i]);
                    node.parent = this.buttonArr[1];
                }else if(line >= 2){
                    let node = cc.instantiate(this.writeButtonPre);
                    this.LabelButtonArr.push(node);
                    node.getComponent("WriteButtonJs").init(newKeyArr[i]);
                    node.parent = this.buttonArr[2];
                }
            }
        }else{
            for(let i=0;i<newKeyArr.length;i++){
                this.LabelButtonArr[i].getComponent("WriteButtonJs").init(newKeyArr[i]);
            }
        }
    }
    
    getkeyWords(arr,number){
        Log("数组",arr,"数量",number)
        if(number>arr.length){
           return;
        }   
        var newArrList=[];    
        for(var i=0;i<number;i++){
            var random=Math.floor(Math.random()*(arr.length-1));
            arr.splice(random, 1);
            newArrList.push(arr[random]);
        }
        Log("返回的数组",JayUtil.shuffle(newArrList));
        return JayUtil.shuffle(newArrList);

    }
    setAnswer(label){
        this.lineArr[this.answerIndex].getChildByName("label").getComponent(cc.Label).string = label;
        this.answerArr[this.answerIndex] = label;
        this.updateAnawer();
    }
    delate(){
        SoundUtil.playEffect("music/click");
        if(this.answerIndex == 0){//第一个就不删
            return;
        }
        if(this.answerIndex == this.lineArr.length-1 && this.answerArr[this.answerIndex] != ""){
            this.lineArr[this.answerIndex].getChildByName("label").getComponent(cc.Label).string = "";
            this.answerArr[this.answerIndex] = "";
            this.updateAnawer();
        }else{
            this.lineArr[this.answerIndex - 1].getChildByName("label").getComponent(cc.Label).string = "";
            this.answerArr[this.answerIndex - 1] = "";
            this.updateAnawer();
        }
    }
    blank(){
        SoundUtil.playEffect("music/click");
        if(this.answerIndex == 0){
            return
        }
        this.lineArr[this.answerIndex - 1].getChildByName("label").getComponent(cc.Label).string = " ";
        this.answerArr[this.answerIndex - 1] = " ";
        this.updateAnawer();
    }
    upperCase(){
        SoundUtil.playEffect("music/click");
        this.isUpperCase = !this.isUpperCase;
        if(this.isUpperCase == true){
            var newArr = [];
            for(let i = 0;i<this.buttonLabel.length;i++){
                let str = this.buttonLabel[i].toUpperCase();
                newArr.push(str);
            }
            this.buttonLabel = newArr
            this.initKey(this.buttonLabel);
        }else{
            var newArr = [];
            for(let i = 0;i<this.buttonLabel.length;i++){
                let str = this.buttonLabel[i].toLowerCase();
                newArr.push(str);
            }
            this.buttonLabel = newArr
            this.initKey(this.buttonLabel);
        }

    }

    checkAswer(){
        if(QuestSpawn.isWrong == true){
            return;
        }
        Log("答案：",this.answerArr,"单词",this.wordArr);
        var number = 0;
        for(let i = 0;i<this.answerArr.length;i++){
            if(this.answerArr[i] == this.wordArr[i]){
                number++;
            }
        }
        if(number == this.answerArr.length){
            SoundUtil.playEffect("music/correct");
            this.gameJs.updateExp(this.questExp);
            this.gameJs.initQuest();
            Log("恭喜啦");
        }else{
            SoundUtil.playEffect("music/wrong");
            if(QuestSpawn.isContest == true){
                this.gameJs.updateExp(0,false)
                this.gameJs.initQuest();
                Log("很遗憾哦");
            }else{
                if(this.gameJs.updateLife){
                    this.gameJs.updateLife();
                    this.gameJs.updateExp(0,false)
                }
            }
        }
    }

    start () {

    }

    // update (dt) {}
}
